package net.sourceforge.cath.logging;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sourceforge.cath.context.Context;
import net.sourceforge.cath.context.GlobalTransaction;
import net.sourceforge.cath.context.LoggingUnit;


public class LogFormatter {
	static SimpleDateFormat dateFormat = new SimpleDateFormat(" [yy.MM.dd kk:dd:ss SSS");
	
	public static String formatStartLocalTransaction(LoggingUnit unit){
		StringBuffer sb = new StringBuffer();
		int depth = Context.depth();

		sb.append(Logger.HEADER);
		sb.append(' ');
		sb.append(unit.getStartTime());
		sb.append(' ');
		sb.append('S');	// start Transaction
		sb.append(" gtxid:");
		sb.append(unit.getTransactionId());	// global transaction id
		for(int i=0;i<depth;i++) sb.append("  ");
		sb.append(" ltxid:");
		sb.append(unit.getLocalTransactionId());
		sb.append(' ');
		sb.append(unit.getSignature().getDeclaringType().getName());
		sb.append(':');
		sb.append(unit.getSignature().getName());
		sb.append(' ');
		sb.append(unit.getNote());
		sb.append('\n');
		return sb.toString();
	}
	public static String formatEndLocalTransaction(LoggingUnit unit){
		StringBuffer sb = new StringBuffer();
		
		int depth = Context.depth();
		sb.append(Logger.HEADER);
		sb.append(' ');
		sb.append(unit.getEndTime());
		sb.append(' ');
		sb.append('E');	// start Transaction
		sb.append(" gtxid:");
		sb.append(unit.getTransactionId());	// global transaction id
		for(int i=0;i<depth;i++) sb.append("  ");
		sb.append(" ltxid:");
		sb.append(unit.getLocalTransactionId());
		sb.append(' ');
		sb.append(unit.getSignature().getDeclaringType().getName());
		sb.append(':');
		sb.append(unit.getSignature().getName());
		long latencyTime = unit.getEndTime() - unit.getStartTime();
		long cpuTime = (unit.getEndCpuTime() - unit.getStartCpuTime()) / 1000000;
		sb.append(" latency:");
		sb.append(latencyTime);
		sb.append("ms");
		sb.append(" cpuTime:");
		sb.append(cpuTime);
		sb.append("ms");
		sb.append(' ');
		sb.append(unit.getNote());
		
		sb.append('\n');
		return sb.toString();
	}
	public static String formatStartGlobalTransaction(GlobalTransaction gtx){
		return formatStartGlobalTransaction(gtx,"");
	}
	
	public static String formatStartGlobalTransaction(GlobalTransaction gtx,String note){
		StringBuffer sb = new StringBuffer();
		sb.append(Logger.HEADER);
		sb.append(' ');
		sb.append(gtx.getStartTime());
		sb.append(" SG");
		sb.append(dateFormat.format(new Date()) );
		sb.append("ms] gtxid:");
		sb.append(gtx.getId());
		sb.append(' ');
		sb.append(gtx.getInitiator());
		if(gtx.getRelationship()!=null){ 
			// if it is child transaction, log parent gtxid to represent relationshop
			sb.append(' ');
			sb.append(gtx.getRelationship());
		}
		sb.append(' ');
		sb.append(note);

		sb.append('\n');
	
		return sb.toString();
	}
	public static String formatEndGlobalTransaction(GlobalTransaction gtx){
		return formatEndGlobalTransaction(gtx,"");
	}
		
	public static String formatEndGlobalTransaction(GlobalTransaction gtx,String note){
		StringBuffer sb = new StringBuffer();
		sb.append(Logger.HEADER);
		sb.append(' ');
		sb.append(gtx.getEndTime());
		sb.append(" EG ");
		sb.append(" gtxid:");
		sb.append(gtx.getId());
		sb.append(' ');
		sb.append(gtx.getInitiator());
		sb.append(" latency:");
		sb.append(gtx.getElaspedTime());
		sb.append("ms ");
		sb.append(" cpuTime:");
		sb.append(gtx.getElapsedCpuTime());
		sb.append("ms");

		sb.append(note);

		sb.append('\n');
		return sb.toString();
	}
	
}
